package com.mayikt.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseResponse;
import com.mayikt.constants.Constants;
import com.mayikt.pay.feign.PayContextFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class PayController {
    @Autowired
    private PayContextFeign payContextFeign;

    @GetMapping("/")
    public String index(){
        return "index";
    }

    @GetMapping("/payHtml")
    public void payHtml(String channelId, String payToken, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        BaseResponse<JSONObject> result = payContextFeign.toPayHtml(channelId, payToken);
        if(!result.getCode().equals(Constants.HTTP_RES_CODE_200)){
            response.getWriter().print("系统错误");
        }
        JSONObject data = result.getData();
        String payHtml = data.getString("payHtml");
        response.getWriter().print(payHtml);
    }
}
